import 'package:flutter/material.dart';
import '../constants.dart';
import './conversation_page.dart';

enum ActionItems { GROUP_CHAT, ADD_FRIEND, QR_SCAN, PAYMENT, HELP }

class NavigationIconView {
  final BottomNavigationBarItem item;
  NavigationIconView(
      {Key key, String title, IconData icon, IconData activeIcon})
      : item = BottomNavigationBarItem(
          icon: Icon(icon),
          activeIcon: Icon(activeIcon),
          title: Text(title),
          backgroundColor: Colors.white,
        );
}

class HomeScreen extends StatefulWidget {
  HomeScreen({Key key}) : super(key: key);
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  PageController _pageController;
  List<NavigationIconView> navigationViews;
  int currentIndex = 0;
  List<Widget> pages;
  @override
  void initState() {
    this.navigationViews = [
      NavigationIconView(
          title: '微信',
          icon: IconData(0xe608, fontFamily: Constants.IconFontFamily),
          activeIcon: IconData(0xe603, fontFamily: Constants.IconFontFamily)),
      NavigationIconView(
          title: '通讯录',
          icon: IconData(0xe601, fontFamily: Constants.IconFontFamily),
          activeIcon: IconData(0xe656, fontFamily: Constants.IconFontFamily)),
      NavigationIconView(
          title: '发现',
          icon: IconData(0xe600, fontFamily: Constants.IconFontFamily),
          activeIcon: IconData(0xe671, fontFamily: Constants.IconFontFamily)),
      NavigationIconView(
          title: '我',
          icon: IconData(0xe6c0, fontFamily: Constants.IconFontFamily),
          activeIcon: IconData(0xe626, fontFamily: Constants.IconFontFamily)),
    ];
    _pageController = PageController(initialPage: this.currentIndex);
    pages = [
      ConversationPage(),
      Container(color: Colors.green),
      Container(color: Colors.blue),
      Container(color: Colors.brown),
    ];
  }

  _buildPopUpMenuItem(int iconName, String title) {
    return Row(
      children: [
        Icon(
          IconData(iconName, fontFamily: Constants.IconFontFamily),
          color: Color(AppColors.AppBarPopupMenuTextColor),
        ),
        Container(width: 12),
        Text(
          title,
          style: TextStyle(
            color: Color(AppColors.AppBarPopupMenuTextColor),
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar bottomNaviBar = BottomNavigationBar(
      items: this.navigationViews.map((NavigationIconView view) {
        return view.item;
      }).toList(),
      currentIndex: this.currentIndex,
      type: BottomNavigationBarType.fixed,
      onTap: (int index) {
        setState(() {
          this.currentIndex = index;
          _pageController.animateToPage(currentIndex,
              duration: Duration(microseconds: 200), curve: Curves.easeInOut);
        });
      },
    );

    return Scaffold(
      //导航栏
      appBar: AppBar(title: Text('微信'), elevation: 0.0, actions: [
        Container(
          padding: const EdgeInsets.only(right: 16.0),
          child: IconButton(
              icon: Icon(
                IconData(0xe65e, fontFamily: Constants.IconFontFamily),
                size: 22,
              ),
              onPressed: () {
                print('点击了搜索按钮');
              }),
        ),
        PopupMenuButton(
          itemBuilder: (BuildContext context) {
            return <PopupMenuItem<ActionItems>>[
              PopupMenuItem(
                child: _buildPopUpMenuItem(0xe69e, "发起群聊"),
                value: ActionItems.GROUP_CHAT,
              ),
              PopupMenuItem(
                child: _buildPopUpMenuItem(0xe638, "添加好友"),
                value: ActionItems.ADD_FRIEND,
              ),
              PopupMenuItem(
                child: _buildPopUpMenuItem(0xe62a, "扫一扫"),
                value: ActionItems.QR_SCAN,
              ),
              PopupMenuItem(
                child: _buildPopUpMenuItem(0xe62a, "收付款"),
                value: ActionItems.PAYMENT,
              ),
              PopupMenuItem(
                child: _buildPopUpMenuItem(0xe63d, "帮助与反馈"),
                value: ActionItems.HELP,
              ),
            ];
          },
          icon: Icon(
            IconData(0xe658, fontFamily: Constants.IconFontFamily),
            size: 22,
          ),
          onSelected: (ActionItems selected) {
            print('点击的是$selected');
          },
        ),
      ]),
      //内容的区域
      body: PageView.builder(
        // ignore: missing_return
        itemBuilder: (BuildContext context, int index) {
          return pages[index];
        },
        controller: _pageController,
        itemCount: pages.length,
        onPageChanged: (int index) {
          setState(() {
            currentIndex = index;
          });
        },
      ),

      //tabbar
      bottomNavigationBar: bottomNaviBar,
    );
  }
}
